function loadXML(xmlObject,xmlText){
	xmlObject.loadXML(xmlText);
	var ret = showXMLError(xmlObject,true);
	if( ret )return ret;
	readSchema(xmlObject);
}
function showXMLError(xml,showError){
	var parseError = xml.parseError;
	if( !parseError.errorCode )return false;
	var msg = parseError.reason+"Line:"+parseError.line+"\nLine position:"+parseError.linepos+"\n"+parseError.srcText;
	if( showError )alert(msg);
	return msg;
}

function makeDS(fields){
	var xml = new ActiveXObject("Microsoft.XMLDOM");
	var root = xmlAddElement(xml,"TABLE");
	xmlAddElement(root,"RS",fields);
	return xml;
}
function xmlAddElement(parent,name,attributes){
	var doc = parent.nodeType == 9 ? parent : parent.ownerDocument;
	var node = parent.appendChild(doc.createElement(name));
	var o;
	for( o in attributes )
		if( attributes[o] !== undefined && attributes[o] !== null )
			node.setAttribute(o,attributes[o]);
	return node;
}
function DataTable(tableName,dataSetName){
	this.tableName = tableName || "DataTable";
	this.dataSetName = dataSetName || "DataSet";
	this.columns = [];
	this.rows = [];
	this.addColumns = function(){
	 for( var i=0; i < arguments.length; i++ )
 	 this.columns.push(arguments[i]);
	}
 this.addColumn = function(columnName){this.addColumns(columnName);}
	this.addRow = function(){
		if( !(arguments[0] instanceof Array) && arguments.length != this.columns.length )
			throw new Error(0,"Number of arguments("+arguments.length+") in addRow(...) must be equal to number of columns("+this.columns.length+") in DataTable!");
		var row = [];
		var fields = arguments[0] instanceof Array ? arguments[0] : arguments;
		for( var i = 0; i < fields.length; i++ )
			row.push(fields[i]);
		this.rows.push(row);
		return row;
	}
	this.getXML = function(){return this.getXMLObject().xml;}
	this.getXMLObject = function(){
		var root = new ActiveXObject("Microsoft.XMLDOM");
		var dataSet = xmlAddElement(root,this.dataSetName);
		for( var i = 0; i < this.rows.length; i++ )
			for( var j = 0, dataTable = xmlAddElement(dataSet,this.tableName); j < this.columns.length; j++ )
				xmlAddElement(dataTable,this.columns[j]).text = this.rows[i][j];
		return root;
	}
}
function readSchema(xml){
	if( !xml.recordset || !xml.recordset(0).Name.match(/(xs:schema|xs:element)/) )return xml;
	var xmlSchema = createXMLElement("_XML_SCHEMA_");
	xmlSchema.loadXML(xml.xml);
	var rs = xmlSchema.recordset;
	//var tableName = rs(0).Value.Fields(0).Value.Fields(0).Value.Fields(0).Value.Fields(0).Value.Fields("Name").Value;
	var rsSchema = rs(0).Value.Fields(0).Value.Fields(0).Value.Fields(0).Value.Fields(0).Value;//.Fields(0).Value.Fields(0).Value;
	while( rsSchema.Fields(0).Name != "xs:attribute" )
		rsSchema = rsSchema.Fields(0).Value
	rsSchema = rsSchema(0).Value;
	var root = new ActiveXObject("Microsoft.XMLDOM");
	var row = xmlAddElement(xmlAddElement(root,"DataSet"),"R");
	for( ; !rsSchema.EOF; rsSchema.MoveNext() )
		row.setAttribute(rsSchema.Fields("Name").Value,"");
	xml.loadXML(root.xml);
	var rsDest = xml.recordset;
	rsDest.Delete();
	//var rsSrc = rs(1).Name == "diffgr:diffgram" ? rs(1).Value.Fields(0).Value.Fields(0).Value : rs(1).Value;
	var rsSrc = rs(1).Type == 136 ? rs(1).Value : null;
	if( rsSrc )updateRS(rsDest,rsSrc);
	return xml;
}
function createXMLElement(id,doc){
	doc = doc || document;
	var xml = doc.all[id] || doc.createElement("XML");
	if( !xml.id )xml.id = id;
	if( !xml.parentElement )doc.body.appendChild(xml);
	return xml
}